<?php

class ConexionDB {

    private $con,$daquery;
    private $config = array(
        'host'  => 'localhost',
        'user'  => 'root',
        'pass'  => 'SexCode',
        'db'    => 'sir2'
    );

    function __construct(){
        $this->con = mysqli_connect($this->config['host'],$this->config['user'],$this->config['pass'],$this->config['db']);
        if (!$this->con){
            die('No se puede conectar a la base de datos:' . mysqli_connect_error());
        }
    }

    function __destruct(){
        mysqli_close($this->con);
    }

    //Genericos ----------------------------------------
    function insert($datos, $tabla){
        $campos          = "";
        $valores         = "";
        $this->daquery   = "INSERT INTO ".$tabla." (";

        foreach($datos as $llave => $valor){
            $campos     .= $llave.',';
            $valores    .= '"'.$valor.'",';

        }
        $this->daquery   .= substr($campos, 0, -1).') VALUES ('.substr($valores, 0, -1).');';
        if ($res = mysqli_query($this->con,$this->daquery)){
            return true;
        }
        else return false;
    }

    function getRows($tabla){
        $data = array();
        $this->daquery  = "SELECT * FROM ".$tabla;
        if ($res = mysqli_query($this->con,$this->daquery)){
            if(mysqli_num_rows($res)){
                while($row = mysqli_fetch_assoc($res)){
                    $data[] = $row;
                }
            }
            return $data;
        }
        else return false;
    }

    function getRowsWhere($tabla,$id,$op,$val){
        $data = array();
        $this->daquery  = "SELECT * FROM ".$tabla." WHERE ".$id.$op.$val;
        if ($res = mysqli_query($this->con,$this->daquery)){
            if(mysqli_num_rows($res)){
                while($row = mysqli_fetch_assoc($res)){
                    $data[] = $row;
                }
            }
            return $data;
        }
        else false;
    }

    function getRowWhere($tabla,$id,$op,$val){
        $data = "";
        $this->daquery  = "SELECT * FROM ".$tabla." WHERE ".$id.$op.$val;
        if ($res = mysqli_query($this->con,$this->daquery)){
            if(mysqli_num_rows($res)){
                while($row = mysqli_fetch_assoc($res)){
                    $data = $row;
                }
            }
           // var_dump($data);
            return $data;
        }
        else false;
    }


    function getRowWhere22t($tabla,$id,$op,$val){
        $data = "";
        $this->daquery  = "SELECT * FROM ".$tabla." WHERE ".$id.$op.$val;
        if ($res = mysqli_query($this->con,$this->daquery)){
            if(mysqli_num_rows($res)){
                while($row = mysqli_fetch_assoc($res)){
                    $data = $row;
                }
            }
            // var_dump($data);
            return $data;
        }
        else false;
    }

    function getColumns($tabla, $columns){
        $data = array();
        $this->daquery  = "SELECT ".$columns." FROM ".$tabla;
        if ($res = mysqli_query($this->con,$this->daquery)){
            if(mysqli_num_rows($res)){
                while($row = mysqli_fetch_assoc($res)){
                    $data[] = $row;
                }
            }
            return $data;

        }
        else return false;
    }

    function getColumnsRange($tabla, $columns, $id, $from, $to){
        $data = array();
        $this->daquery  = "SELECT ".$columns." FROM ".$tabla." WHERE ".$id." >= ".$from." AND ".$id." <= ".$to;
        if ($res = mysqli_query($this->con,$this->daquery)){
            if(mysqli_num_rows($res)){
                while($row = mysqli_fetch_assoc($res)){
                    $data[] = $row;
                }
            }
            return $data;
        }
        else return false;
    }

    function update($tabla, $datos, $id, $val){
        $campos          = "";
        $this->daquery   = "UPDATE ".$tabla." SET ";

        foreach($datos as $llave => $valor){
            $campos     .= $llave.'="'.$valor.'",';
        }
        $this->daquery   .= substr($campos, 0, -1).' WHERE '.$id.'='.$val.';';
        if ($res = mysqli_query($this->con,$this->daquery)){
            return true;
        }
        else return false;
    }
}

?>